/*
 * $Id: QEGeoData.m 249 2008-09-09 13:58:25Z jon $
 * Sleeper
 *
 * Created by Jon Gordon on 3/5/08
 * Copyright Jon Gordon.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#import "QEGeoData.h"

#import "QEProvinceCA.h"
#import "QEStateUS.h"

static NSPredicate *designationIsDistrict;
static NSPredicate *designationIsState;
static NSPredicate *designationIsProvince;
static NSPredicate *designationIsTerritory;
static NSPredicate *designationIsStateOrProvinceOrTerritory;


@implementation QEGeoData : NSObject
+ (void)initialize
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static BOOL initialized = NO;
    if (!initialized) {
        initialized = YES;
        // NSLog(@"  initializing QEGeoData");
        
        [QESubdivision initialize];
        [QEStateUS initialize];
        [QEProvinceCA initialize];
        
        designationIsDistrict = [[NSPredicate predicateWithFormat:@"designation LIKE %@",
                                  qeKeyUSStateDesigDistrict] retain];
        designationIsState = [[NSPredicate predicateWithFormat:@"designation LIKE %@",
                               qeKeyUSStateDesigState] retain];
        designationIsProvince = [[NSPredicate predicateWithFormat:@"designation LIKE %@",
                                  qeKeyCAProvDesigProvince] retain];
        designationIsTerritory = [[NSPredicate predicateWithFormat:@"(designation LIKE %@) || (designation LIKE %@)", 
                                   qeKeyUSStateDesigTerritory,
                                   qeKeyCAProvDesigTerritory] retain];
        designationIsStateOrProvinceOrTerritory = 
        [[NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:designationIsDistrict,
                                                            designationIsState, 
                                                            designationIsProvince, 
                                                            designationIsTerritory, 
                                                            nil]] retain];
        
    } // if
} // initialize


- (NSArray *)allUSAndCAStatesAndTerritories
{
//    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] 
                                                          initWithKey:@"name"
                                                          ascending:YES] autorelease]];
    return [[[self allUSStatesAndTerritories] 
             arrayByAddingObjectsFromArray:[self allCAProvincesAndTerritories]]
            sortedArrayUsingDescriptors:sortDescriptors];
} // allUSAndCAStatesAndTerritories


- (NSArray *)allUSAndCAStatesAndTerritoriesWithAbbr
{
    NSPredicate *notEmpty = [NSPredicate predicateWithFormat:@"(postalAbbr != nil) && not (postalAbbr like '')"];
    NSArray *result = [[self allUSAndCAStatesAndTerritories] filteredArrayUsingPredicate:notEmpty];
    return result;
} // allUSAndCAStatesAndTerritoriesWithAbbr


- (NSArray *)allUSStatesAndDependencies
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [QEStateUS allStates];
} // allUSStatesAndDependencies


- (NSArray *)allUSStatesAndTerritories
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    NSPredicate *predicate =
    [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:designationIsState,
                                                       designationIsDistrict,
                                                       designationIsTerritory,
                                                       nil]];
    return [[QEStateUS allStates] filteredArrayUsingPredicate:predicate];
} // allUSStatesAndTerritories


- (NSArray *)allUSStatesAndDC
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    NSPredicate *predicate =
    [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:designationIsState,
                                                       designationIsDistrict,
                                                       nil]];
    return [[QEStateUS allStates] filteredArrayUsingPredicate:predicate];
} // allUSStatesAndDC


- (NSArray *)allUSStates
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[QEStateUS allStates] filteredArrayUsingPredicate:designationIsState];
} // allUSStates


- (NSArray *)allCAProvincesAndTerritories
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [QEProvinceCA allProvinces];
} // allCAProvincesAndTerritories


- (NSArray *)allCAProvinces
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[QEProvinceCA allProvinces] filteredArrayUsingPredicate:designationIsProvince];
} // allCAProvinces
@end
